package com.jzo2o.foundations.handler;

import com.jzo2o.api.foundations.dto.response.RegionSimpleResDTO;
import com.jzo2o.foundations.constants.RedisConstants;
import com.jzo2o.foundations.model.dto.response.ServeAggregationSimpleResDTO;
import com.jzo2o.foundations.model.dto.response.ServeCategoryResDTO;
import com.jzo2o.foundations.model.dto.response.ServeTypeResDTO;
import com.jzo2o.foundations.service.HomeService;
import com.jzo2o.foundations.service.IRegionService;
import com.jzo2o.foundations.service.IServeService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Mr.M
 * @version 1.0
 * @description 缓存同步任务
 * @date 2024/11/4 14:45
 */
@Component
@Slf4j
public class SpringCacheSyncHandler {

    @Resource
    private IRegionService regionService;

    @Resource
    private HomeService homeService;

    @Resource
    private RedisTemplate redisTemplate;

    //定时更新开通区域列表缓存
    @XxlJob("activeRegionCacheSync")
    public void activeRegionCacheSync() {
        log.info("开始同步区域缓存");
        //key
        String key = RedisConstants.CacheName.JZ_CACHE + "::ACTIVE_REGIONS";

        //删除开通区域列表缓存
        redisTemplate.delete(key);
        //刷新缓存
        List<RegionSimpleResDTO> regionSimpleResDTOS = regionService.queryActiveRegionListCache();

        //遍历regionSimpleResDTOS
        for (RegionSimpleResDTO regionSimpleResDTO : regionSimpleResDTOS) {
            //获取id
            Long regionId = regionSimpleResDTO.getId();
            //删除首页服务列表缓存
            redisTemplate.delete( RedisConstants.CacheName.SERVE_ICON + "::" + regionId);
            //更新首页服务列表缓存
            homeService.queryServeIconCategoryByRegionIdCache(regionId);

        }
    }

    //定时更新区域全部服务类型列表
    @XxlJob("serveTypeCacheSync")
    public void serveTypeCacheSync(){
        log.info("开始同步服务类型缓存");

        // 获取所有开通区域列表
        List<RegionSimpleResDTO> regionSimpleResDTOS = regionService.queryActiveRegionListCache();

        // 遍历 regionSimpleResDTOS
        for (RegionSimpleResDTO regionSimpleResDTO : regionSimpleResDTOS) {
            // 获取 id
            Long regionId = regionSimpleResDTO.getId();
            // key
            String key = RedisConstants.CacheName.JZ_CACHE + "::SERVE_TYPE::" + regionId;

            // 删除服务类型列表缓存
            redisTemplate.delete(key);

            // 刷新缓存
            List<ServeCategoryResDTO> serveCategoryResDTOS = homeService.serveTypeList(regionId);

            // 将服务类型列表存入缓存
            redisTemplate.opsForValue().set(key, serveCategoryResDTOS);

            log.info("区域 {} 的服务类型缓存同步完成", regionId);
        }

        log.info("服务类型缓存同步完成");
    }
    /**
     * 定时更新热门服务列表缓存
     *
     */
    @XxlJob("hotServeCacheSync")
    public void hotServeCacheSync(){
        log.info("开始同步热门服务缓存");

        // 获取所有开通区域列表
        List<RegionSimpleResDTO> regionSimpleResDTOS = regionService.queryActiveRegionListCache();
        // 遍历 regionSimpleResDTOS
        for (RegionSimpleResDTO regionSimpleResDTO : regionSimpleResDTOS) {
            // 获取 id
            Long regionId = regionSimpleResDTO.getId();
            // key
            String hotServeListKey = RedisConstants.CacheName.JZ_CACHE + "::HOT_SERVE::" + regionId;

            // 删除热门服务列表缓存
            redisTemplate.delete(hotServeListKey);

            // 刷新缓存
            List<ServeAggregationSimpleResDTO> serveAggregationSimpleResDTOS = homeService.hotServeList(regionId);
            log.info("========更新该区域下的热门服务列表完成: {}========", hotServeListKey);
            for (ServeAggregationSimpleResDTO serve : serveAggregationSimpleResDTOS) {
                // 删除该区域下的热门服务详情缓存
                String hotServeKey = RedisConstants.CacheName.SERVE + "::" + serve.getId();
                redisTemplate.delete(hotServeKey);
                log.info("========更新该区域下的热门服务完成: {}========", hotServeKey);
                String hotItemKey = RedisConstants.CacheName.SERVE_ITEM + "::" + serve.getServeItemId();
                redisTemplate.delete(hotItemKey);
                log.info("========更新该区域下的热门服务项完成: {}========", hotItemKey);
                homeService.serveDetail(serve.getId());
            }

        }
    }

    }

